package com.lvhaosb.newpneumonia.service.impl;

import com.lvhaosb.newpneumonia.POJO.vo.DomesticCityEpidemicInfo;
import com.lvhaosb.newpneumonia.POJO.po.DomesticCity;
import com.lvhaosb.newpneumonia.POJO.po.DomesticCityEpidemic;
import com.lvhaosb.newpneumonia.mapper.DomesticCityEpidemicMapper;
import com.lvhaosb.newpneumonia.mapper.DomesticCityMapper;
import com.lvhaosb.newpneumonia.service.DomesticCityEpidemicService;
import com.lvhaosb.newpneumonia.tools.ClassTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 国内各大新冠疫情 服务实现类
 *
 * @author lvhao
 * @since 2020-06-21
 */
@Service
public class DomesticCityEpidemicServiceImpl implements DomesticCityEpidemicService {
    @Autowired
    private DomesticCityMapper domesticCityMapper;
    @Autowired
    private DomesticCityEpidemicMapper domesticCityEpidemicMapper;

    @Override
    public List<DomesticCityEpidemicInfo> queryDomesticCityEpidemicInfo() {
        //查询最新的国内各城市疫情信息
        //1. 获取当前有数据的城市
        List<DomesticCity> domesticCities = domesticCityMapper.selectAll();
        //2. 生成返回数据列表
        List<DomesticCityEpidemicInfo> list = new ArrayList<>();
        for(DomesticCity domesticCity: domesticCities){
            //3. 查询城市最新信息并包装数据加入返回列表
            DomesticCityEpidemic domesticCityEpidemic = domesticCityEpidemicMapper.selectByIdAndTimeNewest(domesticCity.getId()).get(0);
            // 赋值
            DomesticCityEpidemicInfo domesticCityEpidemicInfo = new DomesticCityEpidemicInfo();
            domesticCityEpidemicInfo.setCity(domesticCity.getName());
            ClassTool classTool = new ClassTool();
            try {
                classTool.classAssignment(domesticCityEpidemic, domesticCityEpidemicInfo);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            domesticCityEpidemicInfo.setTime(domesticCityEpidemic.getTime().toString());//时间
            // 加入列表
            list.add(domesticCityEpidemicInfo);
        }
        return list;
    }

    @Override
    @Transactional
    public Boolean save(DomesticCityEpidemicInfo domesticCityEpidemicInfo) {
        // 1. 判断城市列表是否有该城市，如果没有则添加该城市到数据库
        if(domesticCityMapper.selectByName(domesticCityEpidemicInfo.getCity()).isEmpty()){
            DomesticCity domesticCity = new DomesticCity();
            domesticCity.setName(domesticCityEpidemicInfo.getCity());
            int insert = domesticCityMapper.insert(domesticCity);
            if(insert>0){
                System.out.println(""+domesticCity.getName()+"城市已添加");
            }
        }
        // 2. 添加该城市的疫情信息
        DomesticCity domesticCity = domesticCityMapper.selectByName(domesticCityEpidemicInfo.getCity()).get(0);
        DomesticCityEpidemic domesticCityEpidemic = new DomesticCityEpidemic();
        ClassTool classTool = new ClassTool();
        try {
            classTool.classAssignment(domesticCityEpidemicInfo, domesticCityEpidemic);
            //System.out.println(domesticCityEpidemicInfo);
            //System.out.println(domesticCityEpidemic);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        domesticCityEpidemic.setTime(LocalDateTime.now());//时间
        domesticCityEpidemic.setCityId(domesticCity.getId());// 城市id
        int insert = domesticCityEpidemicMapper.insert(domesticCityEpidemic);
        if(insert>0){
            return true;
        }
        return false;
    }
}
